/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.strategy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.strategy.entity.StrategyOrderContract;
import com.pig4cloud.pigx.strategy.enums.StrategyOrderContractEnum;
import com.pig4cloud.pigx.strategy.mapper.StrategyOrderContractMapper;
import com.pig4cloud.pigx.strategy.service.StrategyOrderContractService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 策略订单-合约
 *
 * @since 2022-11-24 10:39:26
 */
@Service
public class StrategyOrderContractServiceImpl extends ServiceImpl<StrategyOrderContractMapper, StrategyOrderContract> implements StrategyOrderContractService {

    /**
     * 分页查询
     *
     * @param page      分页数据
     * @param condition 查询条件
     * @return {@link Page < StrategyOrderContract>>}
     * @since 2022/12/13 10:42
     */
    @Override
    public Page<StrategyOrderContract> page(Page<StrategyOrderContract> page, StrategyOrderContract condition) {
        QueryWrapper<StrategyOrderContract> query = new QueryWrapper<>();
        query.lambda()
                .eq(!Objects.isNull(condition.getPlatformId()), StrategyOrderContract::getPlatformId, condition.getPlatformId())
                .eq(!Objects.isNull(condition.getTaskId()), StrategyOrderContract::getTaskId, condition.getTaskId())
                .eq(!Objects.isNull(condition.getPositionId()), StrategyOrderContract::getPositionId, condition.getPositionId())
                .eq(!Objects.isNull(condition.getUserId()), StrategyOrderContract::getUserId, condition.getUserId())
                .eq(Strings.isNotBlank(condition.getProductName()), StrategyOrderContract::getProductName, condition.getProductName())
                .eq(Strings.isNotBlank(condition.getType()), StrategyOrderContract::getType, condition.getType())
                .eq(Strings.isNotBlank(condition.getBusinessType()), StrategyOrderContract::getBusinessType, condition.getBusinessType())
                .eq(Strings.isNotBlank(condition.getSide()), StrategyOrderContract::getSide, condition.getSide())
                .eq(Strings.isNotBlank(condition.getOperationType()), StrategyOrderContract::getOperationType, condition.getOperationType())
                .eq(Strings.isNotBlank(condition.getStatus()), StrategyOrderContract::getStatus, condition.getStatus())
                .orderByDesc(StrategyOrderContract::getCreateTime);
        page = baseMapper.selectPage(page, query);
        List<StrategyOrderContract> list = page.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            return page;
        }

        // 遍历
        list.forEach(i -> {
            // 设置字典项
            i.setType(StrategyOrderContractEnum.Type.findName(i.getType()));
            i.setBusinessType(StrategyOrderContractEnum.BusinessType.findName(i.getBusinessType()));
            i.setPositionMode(StrategyOrderContractEnum.PositionMode.findName(i.getPositionMode()));
            i.setSide(StrategyOrderContractEnum.Side.findName(i.getSide()));
            i.setOperationType(StrategyOrderContractEnum.OperationType.findName(i.getOperationType()));
            i.setStatus(StrategyOrderContractEnum.Status.findName(i.getStatus()));
        });

        return page;
    }
}
